package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

public class CartController {

    private int a;

    /**
     * 1.根据用户id和查询购物车的多条信息
     * 2.根据用户id商家id查询购物车信息
     */
    public Object listCart(HttpServletRequest request) throws Exception{
        // 获取请求参数 CartController/listCart?userId=12345671111&businessId=10001
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        CartService cartService = new CartServiceImpl();
        // 调用service方法
        List<Cart> list = cartService.listCart(userId,businessId);
        return list;
    }

    /**
     * 向购物车添加一条数据
     */
    public Object saveCart(HttpServletRequest request) throws Exception{
        // 获取请求参数 CartController/saveCart?foodId=1&businessId=10001&userId=11111111111&quantity=20
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        Integer quantity = Integer.valueOf(request.getParameter("quantity"));
        Cart cart = new Cart();
        cart.setFoodId(foodId);
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        cart.setQuantity(quantity);
        CartService service = new CartServiceImpl();
        // 调用service方法
        a = service.saveCart(cart);
        // 返回增加购物车影响的行数
        return a;
    }
    /**
     * 根据用户编号、商家编号修改购物车表中的多条条记录
     */
    public Object updateCart(HttpServletRequest request) throws Exception{
        // 获取请求参数 CartController/updateCart?quantity=20&userId=12345672222&businessId=10001&foodId=5
        Integer quantity = Integer.valueOf(request.getParameter("quantity"));
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Cart cart = new Cart();
        cart.setQuantity(quantity);
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        cart.setFoodId(foodId);
        CartService service = new CartServiceImpl();
        // 调用service方法
        a = service.updateCart(cart);
        // 返回修改购物车表数据所影响的行数
        return a;
    }

    /**
     * 根据用户编号、商家编号删除购物车表中的多条条记录
     */
    // 获取请求参数 CartController/removeCart?userId=12345672222&businessId=10003&foodId=14
    public Object removeCart(HttpServletRequest request) throws Exception{
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        CartService service = new CartServiceImpl();
        // 调用service方法
        a = service.removeCart(userId,businessId,foodId);
        // 返回删除购物车表数据影响的行数
        return a;
    }

}
